//
//  WiiEditNotesViewController.m
//  iFeller
//
//  Created by Wii on 15/12/14.
//  Copyright © 2015年 Wii. All rights reserved.
//

#import "WiiEditNotesViewController.h"
#import "WiiKit.h"
@interface WiiEditNotesViewController () <UIViewControllerTransitioningDelegate,UITextViewDelegate>
@property (nonatomic , strong) UIButton *doneButton;
@property (nonatomic , strong) UIImageView *editImageView;
@property (nonatomic , strong) UITextView *editNotesTextView;
@property (nonatomic , strong) UILabel *countLabel;
@property (nonatomic , assign) CGSize keyboardSize;
@end

@implementation WiiEditNotesViewController

- (instancetype)init{
    self = [super init];
    if (self) {
        self.transitioningDelegate = self;
        self.modalPresentationStyle = UIModalPresentationCustom; // 自定义模态推送
    }
    return self;
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillShow:) name:UIKeyboardWillShowNotification object:nil];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUserInterface];
}

- (void)initUserInterface{
    self.view.backgroundColor = [UIColor clearColor];
    
    [self.view addSubview:self.editImageView];
    [self.view addSubview:self.doneButton];
    [self.view addSubview:self.editNotesTextView];
    [self.editNotesTextView addSubview:self.countLabel];
    
}
// 注册键盘通知
- (void)keyBoardWillShow:(NSNotification *)aNoti {
    NSDictionary * info = [aNoti userInfo];
    NSValue * value = [info objectForKey:UIKeyboardFrameBeginUserInfoKey];
    self.keyboardSize = [value CGRectValue].size;
//    if (self.keyboardSize.height > Y) {
        self.view.transform = CGAffineTransformMakeTranslation(WiiZero, - 599);
//    }
    
}

#pragma mark - <UIViewControllerTransitioningDelegate>
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source{
    return [WiiKit transitionWithTransitionType:WiiPresentTransitionPresent];
}
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed{
    return [WiiKit transitionWithTransitionType:WiiPresentTransitionDismiss];
}

#pragma mark -Button handleEvent

- (void)handleEvent:(UIButton *)sender{
    if (self.doneEditNotes) {
        self.doneEditNotes(self.editNotesTextView.text);
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark DoneEditNotes -->Block
- (void)respondsButtonWithDoneEditNotes:(DoneEditNotes)doneEditNotes{
    self.doneEditNotes = doneEditNotes;
}
#pragma mark - UITextViewDelegate
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    if (self.isTaskName == YES) {
        if (textView.text.length >= 12) {
            self.editNotesTextView.text = [textView.text substringToIndex:12];
            [self presentViewController:[WiiKit alertControllerWithMessage:@"字数超过限制！"] animated:YES completion:nil];
            return NO;
        } else{
            return YES;
        }
    } else {
        if (range.location >= 120) {
            self.editNotesTextView.text = [textView.text substringToIndex:120];
            [self presentViewController:[WiiKit alertControllerWithMessage:@"字数超过限制!"] animated:YES completion:nil
             ];
            return NO;
        } else
        {
            return YES;
        }
    }
}
- (void)textViewDidChange:(UITextView *)textView{
    if (self.isTaskName == YES) {
        [self.countLabel setText:[NSString stringWithFormat:@"%ld/12",[textView.text length]]];
    } else {
        [self.countLabel setText:[NSString stringWithFormat:@"%ld/120",[textView.text length]]];
    }
}
#pragma mark － setter/getter
- (UIButton *)doneButton{
    if (!_doneButton) {
        _doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _doneButton.frame = CGRectMake(SCREEN_WIDTH - 80, 5, 60, 30);
        [_doneButton setTitle:@"Done" forState:UIControlStateNormal];
        [_doneButton addTarget:self action:@selector(handleEvent:) forControlEvents:UIControlEventTouchUpInside];
        [_doneButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        
    }
    return _doneButton;
}
- (UIImageView *)editImageView{
    if (!_editImageView) {
        _editImageView = [[UIImageView alloc]initWithFrame:CGRectMake(20, 5, WiiButtonSize, WiiButtonSize)];
        _editImageView.image = [UIImage imageNamed:@"Edit"];
    }
    return _editImageView;
}

- (UITextView *)editNotesTextView{
    if (!_editNotesTextView) {
        _editNotesTextView = [[UITextView alloc]initWithFrame:CGRectMake(10, 30, SCREEN_WIDTH - 20, SCREEN_HEIGHT / 2 - 30)];
        _editNotesTextView.font = [UIFont systemFontOfSize:14];
        _editNotesTextView.layer.cornerRadius = 20;
        _editNotesTextView.backgroundColor = [UIColor colorWithRed:0.992 green:0.961 blue:0.863 alpha:1.000];
        _editNotesTextView.returnKeyType = UIReturnKeyDone;
        _editNotesTextView.text = self.notesText;
        _editNotesTextView.delegate = self;
    }
    return _editNotesTextView;
}
- (UILabel *)countLabel{
    if (!_countLabel) {
        _countLabel = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH - 120,CGRectGetHeight(_editNotesTextView.frame) - 50, 120, 40)];
        _countLabel.font = [UIFont systemFontOfSize:21];
        [_countLabel setTextColor:[UIColor blackColor]];
        _countLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _countLabel;
}
@end
